<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/04/0004
 * Time: 13:15
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class Student extends Model
{
    use SoftDelete;
    public $pk = 'id';

    public $table = 'students';

    public $autoWriteTimestamp = true;

    public $hidden = ['update_time','create_time'];

    public function getStuSexAttr($value)
    {
        return $value == 1 ? '男' : "女";
    }

    public function teacher()
    {
        return $this->belongsTo('teacher','teacher_id','id')->field('teacher_name,id');
    }
    public function school()
    {
        return $this->belongsTo('school','school_id','school_id')->field('school_id,school_name');
    }
    /**
     * 查看学生信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $schoolId 学校id
     * @param $teacherId 教师id
     */
    public static function getStudentList($page,$rows,$sort,$sortOrder,$searchName,$schoolId,$teacherId)
    {
        $result = self::where(function($query)use($searchName){
                if($searchName){
                    $query->where('stu_name|parent_phone|stu_no','like',"%{$searchName}%");
                }
            })->where('teacher_id',$teacherId)->where('school_id',$schoolId)->order("{$sort}","{$sortOrder}")
            ->page($page)->limit($rows)->select();
        $count = self::where(function($query)use($searchName){
                if($searchName){
                    $query->where('stu_name|parent_phone|stu_no','like',"%{$searchName}%");
                }
            })->where('teacher_id',$teacherId)->where('school_id',$schoolId)->count();

        return json(['rows'=>$result,'total'=>$count]);
    }

    /**
     * 家长子女列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $parentId 家长id
     */
    public static function getChildrenList($page,$rows,$sort,$sortOrder,$searchName,$parentId)
    {
        $result = self::with(['teacher'])->where(function($query)use($searchName){
            if($searchName){
                $query->where('stu_name|stu_no','like',"%{$searchName}%");
            }
        })->where('parent_id',$parentId)->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $count = self::with(['teacher'])->where(function($query)use($searchName){
            if($searchName){
                $query->where('stu_name|stu_no','like',"%{$searchName}%");
            }
        })->where('parent_id',$parentId)->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
}